package weiyao.xinxidasai.service.model;

import com.aliyun.oss.model.PartETag;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Getter
@Setter
public class ChunkInfo {
    private String uniqueIdentifier; // 新增唯一标识符字段
    private String md5;
    private String uploadId;
    private int chunkTotal;
    private long standardChunkSize;
    private long lastChunkSize;
    private long totalFileSize;
    private String fileName;
    private Long folderId;
    private Long versionId;
    private String encryptionType;
    private Long userId;
    private Long filesId; // 新增文件ID字段
    private String secretKey;
    private String mimeType;
    private String fileType; // 新增文件类型字段
    @JsonProperty("objectKey")
    private String objectKey;

    private Map<Integer, String> partEtags = new ConcurrentHashMap<>();

    public void addPartEtag(int partNumber, String eTag) {
        partEtags.put(partNumber, eTag);
    }

    public Map<Integer, String> getPartEtagsMap() {
        return partEtags;
    }

    public void setPartEtagsMap(Map<Integer, String> partEtags) {
        this.partEtags = partEtags;
    }

    @JsonIgnore
    public List<PartETag> getPartEtags() {
        return partEtags.entrySet().stream()
                .map(e -> new PartETag(e.getKey(), e.getValue()))
                .collect(Collectors.toList());
    }

    // 确保这个方法不参与序列化
    @JsonIgnore
    public long getTotalFileSize() {
        if (totalFileSize > 0) {
            return totalFileSize;
        }
        if (chunkTotal == 1) {
            return lastChunkSize;
        }
        return (chunkTotal - 1) * standardChunkSize + lastChunkSize;
    }
}